// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
import QtQuick 1.1
import com.nokia.meego 1.0
import com.nokia.extras 1.0

Item {
    function reloadData() {
        listView.header = reloadingHeader
        pagesModel.clear()
        rosYama.listHoles()
        removeReloading.start()
    }

    Component.onCompleted: reloadData()

    ListModel { id: pagesModel }

    PageHeader {
        id: header
        text: qsTr("Defect list")
        BusyIndicator {
            id: busyIndicator
            running: false
            visible: false
            anchors {
                verticalCenter: parent.verticalCenter
                right: parent.right
                margins: UiConstants.DefaultMargin
            }
        }
    }

    Timer {
        id: time
        interval: 1000; running: false; repeat: false
        onTriggered: reloadData()
    }
    Timer {
        id: removeReloading
        interval: 5000; running: false; repeat: false
        onTriggered: { listView.header = listHeader }
    }

    Component {
        id: reloadingHeader

        Item {
            width: listView.width
            height:  50

            Rectangle {
                height: 200
                width: parent.width + UiConstants.DefaultMargin*2
                color: "darkgray"

                anchors {
                    bottom: parent.bottom
                    bottomMargin: 10
                    horizontalCenter: parent.horizontalCenter
                }

                Row {
                    width: listView.width
                    anchors.horizontalCenter: parent.horizontalCenter
                    anchors.bottom: parent.bottom
                    anchors.bottomMargin: 10
                    spacing: 10

                    BusyIndicator { running: true; platformStyle: BusyIndicatorStyle { inverted: true }}
                    Label { text: qsTr("Reloading"); platformStyle: LabelStyle { inverted: true }}
                }
            }
        }
    }
    Component {
        id: listHeader

        Item {
            width: listView.width
            height:  0
            visible: -listView.contentY > 10;

            Rectangle {
                height: 200
                width: parent.width + UiConstants.DefaultMargin*2
                color: "darkgray"

                anchors {
                    bottom: parent.top
                    bottomMargin: 10
                    horizontalCenter: parent.horizontalCenter
                }

                Row {
                    width: listView.width
                    anchors.horizontalCenter: parent.horizontalCenter
                    anchors.bottom: parent.bottom
                    anchors.bottomMargin: 10
                    spacing: 10
                    Image {
                        source: "image://theme/icon-s-input-method-down"
                    }
                    Label {
                        text: qsTr("Pull to reload")
                        platformStyle: LabelStyle { inverted: true }
                    }

                    onVisibleChanged: {
                        if(visible) {
                            time.start()
                        } else {
                            time.stop()
                        }
                    }
                }
            }

        }
    }

    ListView {
        id: listView
        anchors {
            left: parent.left
            right: parent.right
            top: header.bottom
            bottom: parent.bottom
            margins: UiConstants.DefaultMargin
        }
        header: listHeader
        spacing: 10
        model: pagesModel
        delegate:  HoleDelegate {}
    }
    ScrollDecorator { flickableItem: listView }
    Connections {
        target: rosYama
        onListHolesResult: {
            pagesModel.append({"address": address, "status": status, "type": type, "date": date, "image": image})
        }
    }
}
